package com.woniu.microprojectserver.spacemag.controller;

import com.woniu.microcommonsserver.entity.BuildInfoEntity;
import com.woniu.microcommonsserver.entity.LpersonInfoEntity;
import com.woniu.microcommonsserver.entity.SpaceInfoEntity;
import com.woniu.microcommonsserver.param.ResponseMsg;
import com.woniu.microprojectserver.bean.BuildSpace;
import com.woniu.microprojectserver.bean.BuildTreeUtils;
import com.woniu.microprojectserver.bean.SpacePerson;
import com.woniu.microprojectserver.bean.TreeData;
import com.woniu.microprojectserver.buildmag.service.impl.BuildServiceImpl;
import com.woniu.microprojectserver.personmag.service.impl.PersonServiceImpl;
import com.woniu.microprojectserver.spacemag.service.impl.SpaceServiceImpl;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/space")
public class SpaceController {

    @Resource
    private BuildServiceImpl buildService;

    @Resource
    private SpaceServiceImpl spaceService;

    @Resource
    private PersonServiceImpl personService;

    @GetMapping(value = "/findBandS",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg findBandS(){
        List list = new ArrayList();
        list.add(buildService.findAll());
        list.add(spaceService.findAll());
        return ResponseMsg.builder().code(200).msg("查询成功").data(list).build();
    }


    @GetMapping(value = "/findAll",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg findAll(){
        List<BuildInfoEntity> buildList = buildService.findAll();
        List<SpaceInfoEntity> spaceList = spaceService.findAll();
        List<TreeData> treeList = new ArrayList<>();
        for (int i = 0; i < buildList.size(); i++) {
            BuildInfoEntity a = buildList.get(i);
            if (a.getRank().equals(1L)) {
                TreeData td = new TreeData();
                td.setId(a.getId());
                td.setLabel(a.getName());
                td.setR1(true);
                td.setSpace(false);
                td.setHigherId(a.getHigherId());
                List<TreeData> children = new BuildTreeUtils().getAllTree(td,buildList,spaceList);
                for (int s = 0; s < spaceList.size(); s++) {
                    SpaceInfoEntity space = spaceList.get(s);
                    if(space.getFkBuildId().equals(td.getId())){
                        TreeData td2 = new TreeData();
                        td2.setId(space.getId());
                        td2.setLabel(space.getAbbreviation());
                        td2.setFkBuildId(space.getFkBuildId());
                        td2.setHigherId(space.getHigherId());
                        td2.setSpace(true);
                        td2.setFloor(space.getFloor());
                        td2.setChildren(new BuildTreeUtils().getSpaceTree(space.getId(),spaceList));
                        children.add(td2);
                    }
                }
                td.setChildren(children);
                treeList.add(td);
            }
        }

        return ResponseMsg.builder().code(200).msg("查询成功").data(treeList).build();
    }

    @GetMapping(value = "/findById",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg findById(String id){
        SpaceInfoEntity space = spaceService.findById(id);
        List<LpersonInfoEntity> list = personService.findBySpaceId(id);
        SpacePerson spacePerson = new SpacePerson();
        spacePerson.setSpace(space);
        spacePerson.setPeople(list);
        return ResponseMsg.builder().code(200).msg("查询成功").data(spacePerson).build();
    }

    @PostMapping(value = "/add",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg add(@RequestBody SpaceInfoEntity spaceInfoEntity){
        boolean bool = spaceService.add(spaceInfoEntity);
        return ResponseMsg.builder().code(200).msg("添加成功").data(bool).build();
    }

    @PostMapping(value = "/update",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg update(@RequestBody SpaceInfoEntity spaceInfoEntity){
        boolean bool = spaceService.update(spaceInfoEntity);
        return ResponseMsg.builder().code(200).msg("修改成功").data(bool).build();
    }

    @GetMapping(value = "/delete",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg delete(String id){
        boolean b = deleteDo(id);
        return ResponseMsg.builder().code(200).msg("删除成功").data(b).build();
    }

    public boolean deleteDo(String id){
        List<SpaceInfoEntity> list = spaceService.findByHId(id);
        if(list != null && list.size() != 0 ){
            for (SpaceInfoEntity spaceInfoEntity : list) {
                deleteDo("" + spaceInfoEntity.getId());
            }
        }
        boolean a =personService.deleteBySpace(id);
        if(a){
            spaceService.delete(id);
        }
        return true;
    }
}
